﻿using System;
using System.Collections.Generic;
using System.Text;
using KKCai.HNKL.DAL;
using KKCai.HNKL.Model;

namespace KKCai.HNKL.BLL
{
    public class LotteryManager
    {
        /// <summary>
        /// 添加一期开奖记录
        /// </summary>
        public void Save(string phase, string result)
        {
            HNKLInfo r = new HNKLInfo(phase, result);

            new HNKLDao().Save(r);

            if (r.ID != 0)
            {
                GlobalData.Refresh(r);
            }

            new A1Dao().UpdateWinOmit(r);
            new A2Dao().UpdateWinOmit(r);
            new A3Dao().UpdateWinOmit(r);
            new P2Dao().UpdateWinOmit(r);
            new P3Dao().UpdateWinOmit(r);
            new N2Dao().UpdateWinOmit(r);
            new N3Dao().UpdateWinOmit(r);
            new N4Dao().UpdateWinOmit(r);
            new N5Dao().UpdateWinOmit(r);
        }


        private const string ID = "id";
        private const string MaxOmit = "MaxOmit";
        private const string CurrentOmit = "LastID";
        private const string Date = "MaxOmitQH";
        private const string TotalCount = "TotalCount";

        public object List(string mode, string order, int start, int limit)
        {
            string orderby = CurrentOmit;
            bool asc = true;

            if (!String.IsNullOrEmpty(order))
            {
                switch (order.ToUpper())
                {
                    case "ID": orderby = ID;
                        break;
                    case "MAXOMIT": orderby = MaxOmit; asc = false;
                        break;
                    case "CURRENTOMIT": orderby = CurrentOmit;
                        break;
                    case "DATE": orderby = Date; asc = false;
                        break;
                    case "TOTALCOUNT": orderby = TotalCount;
                        break;
                    default:
                        break;
                }
            }

            switch (mode.ToUpper())
            {
                case "A1": return new A1Dao().Order(orderby, asc, start, limit);
                case "A2": return new A2Dao().Order(orderby, asc, start, limit);
                case "A3": return new A3Dao().Order(orderby, asc, start, limit);
                case "P2": return new P2Dao().Order(orderby, asc, start, limit);
                case "P3": return new P3Dao().Order(orderby, asc, start, limit);
                case "N2": return new N2Dao().Order(orderby, asc, start, limit);
                case "N3": return new N3Dao().Order(orderby, asc, start, limit);
                case "N4": return new N4Dao().Order(orderby, asc, start, limit);
                case "N5": return new N5Dao().Order(orderby, asc, start, limit);
                default:
                    return null;
            }
        }
    }
}
